package com.peam.web.controller.activiti;

import com.peam.common.annotation.Log;
import com.peam.common.core.controller.BaseController;
import com.peam.common.core.domain.AjaxResult;
import com.peam.common.core.domain.entity.SysUser;
import com.peam.common.core.page.TableDataInfo;
import com.peam.common.enums.BusinessType;
import com.peam.common.utils.poi.ExcelUtil;
import com.peam.system.domain.Engineering;
import com.peam.system.service.IEngineeringService;
import com.peam.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static org.apache.naming.SelectorContext.prefix;

/**
 * 工程信息操作处理
 * 
 * @author peam
 */
@RestController
@RequestMapping("/engineering")
public class EngineeringController extends BaseController
{
    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 获取工程列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Engineering engineering)
    {
        String engineeringcode = engineering.getEngineeringCode();
        engineering.setEngineeringCode(engineeringcode);
        startPage();
        List<Engineering> list = engineeringService.selectEngineeringList(engineering);
        return getDataTable(list);
    }
    /**
     * 导出工程列表
     */
    @Log(title = "工程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody

    public void export(HttpServletResponse response, Engineering engineering)
    {
        List<Engineering> list = engineeringService.selectEngineeringList(engineering);
        ExcelUtil<Engineering> util = new ExcelUtil<Engineering>(Engineering.class);
        util.exportExcel(response, list, "工程数据");
    }
    /**
     * 新增工程
     */
//    @GetMapping("/add")
//    public String add(ModelMap mmap)
//    {
//        SysUser user = getLoginUser().getUser();
//
//        mmap.put("user", user);
//        mmap.put("userlist", userService.selectUserList(new SysUser()));
//        return prefix + "/add";
    @PreAuthorize("@ss.hasPermi('engineering:add')")
    @Log(title = "工程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Engineering engineering)
    {
        engineering.setCreateBy(engineering.getEngineeringCode());
        return toAjax(engineeringService.insertEngineering(engineering));
    }
    @Log(title = "新建工程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( @RequestBody Engineering engineering)

    {
        engineering.setCreateTime(new Date());
        return toAjax(engineeringService.insertEngineering(engineering));
    }
    /**
     * 修改工程
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(Engineering engineering)
    {
        return toAjax(engineeringService.updateEngineering(engineering));
    }
//    @PreAuthorize("@ss.hasPermi('engineering:edit')")
//    @Log(title = "工程管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@Validated @RequestBody Engineering engineering)
//    {
//        if (!engineeringService.checkEngineeringNameUnique(engineering))
//        {
//            return error("修改工程'" + engineering.getEngineeringName() + "'失败，工程名称已存在");
//        }
//        engineering.setUpdateBy(getUsername());
//        return toAjax(engineeringService.updateEngineering(engineering));
//    }

    /**
     * 删除工程
     */
    @Log(title = "请假", businessType = BusinessType.DELETE)
    @DeleteMapping( "/remove/{engineeringIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable String engineeringIds)
    {
        engineeringService.deleteEngineeringByIds(engineeringIds);
        return AjaxResult.success("删除成功");
    }
}
